package com.mxx.common.netty.config;


import com.mxx.common.netty.NettyWebsocketOperator;
import com.mxx.common.netty.exception.ExceptionCatcherByWebsocket;
import com.mxx.common.netty.exception.ExceptionCatcherByWebsocketImpl;
import com.mxx.common.netty.ws.*;
import com.mxx.common.conv.JacksonConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/7/17 18:10
 * @Description: 先加载Http组件类 ，后加载Websocket 组件类
 */
@Configuration
@Import(LoadNettyHttpComponent.class)
public class LoadNettyWebsocketComponent {
    /**
     * websocket 异常处理类注入
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(ExceptionCatcherByWebsocket.class)
    public ExceptionCatcherByWebsocket exceptionCatcherByWebsocketImpl() {
        return new ExceptionCatcherByWebsocketImpl();
    }

    /**
     * websocket 处理器操作类注入
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(NettyWebsocketOperator.class)
    public NettyWebsocketOperator defaultNettyWebsocketOperator() {
        return new DefaultNettyWebsocketOperator();
    }

    @Bean
    @ConditionalOnMissingBean(WebsocketFactory.class)
    public WebsocketFactory websocketFactory(JacksonConverter jacksonConverter) {
        return new DefaultWebsocketFactory(jacksonConverter);
    }

    /**
     * websocket 处理器注入
     *
     * @param websocketOperator
     * @return
     */
    @Bean
    public NettyWebsocketHandler nettyWebsocketHandler(NettyWebsocketOperator websocketOperator) {
        return new NettyWebsocketHandler(websocketOperator);
    }

    /**
     * websocket 连接转发处理器
     *
     * @return
     */
    @Bean
    public NettyWebsocketConnectionForwardHandler nettyWebsocketConnectionHttpHandler() {
        return new NettyWebsocketConnectionForwardHandler();
    }


}
